﻿using System;
using System.Net;
using System.Windows;
#if (WINRT)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using System.ComponentModel;
using System.Windows.Interactivity;
using Windows.UI.Xaml.Data;
#else
using System.Windows.Markup;
#endif

namespace nRoute.Components
{
#if (WINRT)
    [ContentProperty(Name = "UrlParameters")]
#else
    [ContentProperty("UrlParameters")]
#endif
    public class NavigationCommand
        : VisualCommandBase
    {
        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string),
            typeof(NavigationCommand), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty UrlParametersProperty = DependencyProperty.Register("UrlParameters", typeof(DependencyParameterCollection),
            typeof(NavigationCommand), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty HandlerNameProperty = DependencyProperty.Register("HandlerName", typeof(string),
            typeof(NavigationCommand), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty SiteAreaProperty = DependencyProperty.Register("SiteArea", typeof(string),
            typeof(NavigationCommand), new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

#region Properties

		public string Url
		{
			get { return Convert.ToString(GetValue(UrlProperty)); }
			set { SetValue(UrlProperty, value); }
		}

        public DependencyParameterCollection UrlParameters
		{
            get { return (DependencyParameterCollection)GetValue(UrlParametersProperty); }
			set { SetValue(UrlParametersProperty, value); }
		}

		public string HandlerName
		{
			get { return Convert.ToString(GetValue(HandlerNameProperty)); }
			set { SetValue(HandlerNameProperty, value); }
		}

		public string SiteArea
		{
			get { return Convert.ToString(GetValue(SiteAreaProperty)); }
			set { SetValue(SiteAreaProperty, value); }
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding UrlBinding
        {
            get { return GetBinding(UrlProperty); }
            set { SetBinding<string>(UrlProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding UrlParametersBinding
        {
            get { return GetBinding(UrlParametersProperty); }
            set { SetBinding<string>(UrlParametersProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding HandlerNameBinding
        {
            get { return GetBinding(HandlerNameProperty); }
            set { SetBinding<string>(HandlerNameProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding SiteAreaBinding
        {
            get { return GetBinding(SiteAreaProperty); }
            set { SetBinding<string>(SiteAreaProperty, value); }
        }

#endif

#endregion

#region Overrides

        public override void Execute()
        {
            if (!string.IsNullOrWhiteSpace(Url))
            {
                var _navigationRequest = new Navigation.NavigationRequest(this.Url, this.UrlParameters, this.SiteArea);
                if (!string.IsNullOrWhiteSpace(this.HandlerName))
                {
                    var _handler = Navigation.NavigationService.GetNavigationHandler(this.HandlerName);
                    Navigation.NavigationService.Navigate(_navigationRequest, _handler);
                }
                else
                {
                    Navigation.NavigationService.Navigate(_navigationRequest);
                }
            }
        }

#endregion

    }
}
